$(function() {
	DiscountMemberDetailsApp.init();
	DiscountMemberDetailsApp.countdown();
});
var DiscountMemberDetailsApp = function(){
	var pageSize = 10; //每页显示条数初始化，修改显示条数，修改这里即可
	var pCount = 0;
	var pagecur;
	var a=0;         //以毫秒为单位
	var bargain_layer;
	return{
		//初始化js
		init:function(){
				a=$("#COUNT_DOWN_TIME").val();//初始化倒计时
		},
		//砍价倒计时
		countdown:function(){
			if(a<=0){
				return false;
			}
		        a=a-1000;
		        var b=new Date();
		        b.setTime(0);
		        var c=new Date();
		        c.setTime(a);
		        var day1=b.getDate();        //为方便调用，把天数、小时等单独定义
		        var hours1=b.getHours();
		        var minu1=b.getMinutes();
		        var seco1=b.getSeconds();
		        var day2=c.getDate();
		        var hours2=c.getHours();
		        var minu2=c.getMinutes();
		        var seco2=c.getSeconds();
		        var day=day2-day1;
		        var hours=hours2-hours1;
		        var minu=minu2-minu1;
		        var seco=seco2-seco1;
		        hours=hours+day*24;
//		        document.getElementById('daya').innerHTML=day;
		        document.getElementById('hoursa').innerHTML=hours;
		        document.getElementById('minua').innerHTML=minu;
		        document.getElementById('secoa').innerHTML=seco;
		        setTimeout("DiscountMemberDetailsApp.countdown()",1000);
		},
		//立即购买
		buynow:function(id){
			window.location.href=$("#APP_ROOT_PATH").val() + "order/discount_confirm?discountId="+id
		},
		subFun:function(id){
			mui.util.Ajax.asyncPOST(
				$("#APP_ROOT_PATH").val() + "discount/bargain",
				{
					"discountid":id,
				},
				function(data){
				if(data==true){
							mui.util.Ajax.asyncPOST(
							$("#APP_ROOT_PATH").val() + "discount/price",
							{
								"discountid":id,
							},
							function(data){
								if(data.ret==null){
									if(data.price!=null){
										DiscountMemberDetailsApp.modal(data.price);
									}
								}
							},
							function(){},
							function(){},
							function(){}
							);
					}else{
						layer.open({
						    content: data.message,
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
					});
					}
				},
				function(){
					layer.open({
						    content: '保存出错！',
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
					});
				},
				function(){},
				function(){
//					setTimeout(function(){
//						layer.closeAll();
//					},1000);
					
				}
			)
			return false;
		},
		bargain:function(id){
//			layer.open({
//				  type: 2,
//				  title: '验证手机',
//				  shadeClose: true,
//				  area: ['100%', '50%'],
//				  shade: 0.3,
//				  content: $("#APP_ROOT_PATH").val() + "discount/toCode?discountid="+id
//				});
		var url = $("#APP_ROOT_PATH").val() + "discount/toCode?discountid="+id
		createXmlHttp(); //创建XMLHttpRequest对象 
		var html;
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) { 
			html =xmlHttp.responseText; 
			bargain_layer=layer.open({  
		    type: 1,  
		    content: html	
		    ,anim: 'up'
  			,style: 'position:fixed; left:0; top:0; width:100%; height:100%; border: none; -webkit-animation-duration: .5s; animation-duration: .5s;'
			});
			CodeApp.init();
			} 
			 
		}; //设置回调函数
		xmlHttp.open("GET", url, true); 
		xmlHttp.send(null); 
	
		
		}, 
		//分享活动
		share:function(id){
			$("#modal-container-1").modal();
		},
		modal:function(id){
//			$("#money").html(id);
//			$("#modal-2").click();
//			layer.closeAll();
//			$("#modal-container-2").modal();
			layer.close(bargain_layer);
			var str='您已经成功帮他砍至'+id+'元。'
			layer.open({
			    content: str
			    ,btn: '我知道了'
			    ,yes: function(index){
				  window.location.reload();
				}    
			  });
		},
		//显示抢购规则
		rule:function(e){
			$(".content-a2").removeClass("active");
			e.addClass("active");
			$("#guize").show();
			$("#gongxian").hide();
		},
		//显示好友贡献
		contribution:function(e){
			$(".content-a2").removeClass("active");
			e.addClass("active");
			$("#gongxian").show();
			$("#guize").hide();
		},
		//刷新
		flash:function(){
			location.reload(true);
		},
	}
}();

function createXmlHttp() { 
//根据window.XMLHttpRequest对象是否存在使用不同的创建方式 
if (window.XMLHttpRequest) { 
xmlHttp = new XMLHttpRequest(); //FireFox、Opera等浏览器支持的创建方式 
} else { 
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式 
} 
} 